﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace lcmyjobs.Web.enprise
{
    public partial class Info : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Application["webname"] != null)
                {
                    this.Title = string.Format("单位管理中心—单位资料：单位信息&bull;联系方式  {0}", Application["webname"].ToString());
                }
                if (Session["enterprise_bid"] != null)
                {
                    int enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                    ShowInfo(enterprise_bid);
                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登陆！", "EnterpriseLogin.html");
                }
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            if (Session["enterprise_bid"] != null)
            {
                int enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                Update(enterprise_bid);
            }
        }

        private void ShowInfo(int enterprise_bid)
        {
            lcmyjobs.BLL.enterprise_basic bll = new lcmyjobs.BLL.enterprise_basic();
            DataSet ds = bll.Get_enterprise_basic(enterprise_bid);

            DataTable dt_basic = ds.Tables[0];
            DataTable dt_trade = ds.Tables[1];

            if (dt_trade.Rows.Count > 0)
            {
                string tradename = "";

                for (int i = 0; i < dt_trade.Rows.Count; i++)
                {
                    tradename += dt_trade.Rows[i]["tradename"].ToString() + ",";
                }

                this.txttrade.Text = tradename.Substring(0, tradename.Length - 1);

            }

            if (dt_basic.Rows.Count > 0)
            {
                this.hid_bid.Value = dt_basic.Rows[0]["enterprise_bid"].ToString();
                this.txtname.Text = dt_basic.Rows[0]["name"].ToString();
                this.txthuji.Text = dt_basic.Rows[0]["area_name"].ToString();
                this.ddl_dwxz.SelectedValue = dt_basic.Rows[0]["dwxz"].ToString();
                this.ddldwgm.SelectedValue = dt_basic.Rows[0]["dwgm"].ToString();
                this.txtclnf.Text = dt_basic.Rows[0]["clnf"].ToString();
                this.txtlxr.Text = dt_basic.Rows[0]["lxr"].ToString();
                this.txttelephone.Text = dt_basic.Rows[0]["telephone"].ToString();
                //this.txtfax.Text = dt_basic.Rows[0]["fax"].ToString();
                this.txtmobilephone.Text = dt_basic.Rows[0]["mobilephone"].ToString();
                this.txtemail.Text = dt_basic.Rows[0]["email"].ToString();
                this.txtwebsite.Text = dt_basic.Rows[0]["website"].ToString();               
                this.txtaddress.Text = dt_basic.Rows[0]["address"].ToString();                
                this.txtabout.Text = dt_basic.Rows[0]["about"].ToString();

                this.lbl_title.Text = " " + dt_basic.Rows[0]["name"].ToString();

            }
        }
        private void Update(int enterprise_bid)
        {
            string name = this.txtname.Text;
            //int areas_id = int.Parse(this.txtareas_id.Text);
            string area_name = this.txthuji.Text;
            string dwxz = this.ddl_dwxz.SelectedItem.Value;
            string dwgm = this.ddldwgm.SelectedItem.Value;
            string clnf = this.txtclnf.Text;
            string lxr = this.txtlxr.Text;
            string telephone = this.txttelephone.Text;
            string email = this.txtemail.Text;
            //string fax = this.txtfax.Text;
            string fax = "";
            string mobilephone = this.txtmobilephone.Text;
            string website = this.txtwebsite.Text;
            string place = this.txtaddress.Text;
            string address = this.txtaddress.Text;
            string interviewadd = this.txtaddress.Text;
            string about = this.txtabout.Text;           
            string[] hys = this.txttrade.Text.Split(',');//行业
            string tradename1 = "0";
            string tradename2 = "0";
            string tradename3 = "0";
            int atid = Convert.ToInt32(Application["webid"]);//注册网站
            if (hys.Length == 0)
            {
                tradename1 = this.txttrade.Text;
            }
            if (hys.Length == 1)
            {
                tradename1 = hys[0];
            }
            if (hys.Length == 2)
            {
                tradename1 = hys[0];
                tradename2 = hys[1];
            }
            if (hys.Length == 3)
            {
                tradename1 = hys[0];
                tradename2 = hys[1];
                tradename3 = hys[2];
            }

            if (this.txtabout.Text.Length < 50)
            {
                Maticsoft.Common.MessageBox.Show(this, "对不起，您的单位介绍过于简短，请填写50字以上。");
            }
            else
            {
                about = Regex.Replace(about, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);  //去除脚本标签
                lcmyjobs.Model.enterprise_basic model = new lcmyjobs.Model.enterprise_basic();
                model.enterprise_bid = enterprise_bid;
                //model.enterprise_bh = enterprise_bh;
                //model.login = login;
                //model.pwd = pwd;
                model.name = name;
                //model.areas_id = areas_id;
                model.dwxz = dwxz;
                model.dwgm = dwgm;
                model.clnf = clnf;
                model.lxr = lxr;
                model.telephone = telephone;
                model.email = email;
                model.fax = fax;
                model.mobilephone = mobilephone;
                model.website = website;
                model.place = place;
                model.address = address;
                model.interviewadd = interviewadd;
                model.about = about;
                model.areaname = area_name;//所属地区名
                model.tradename1 = tradename1;
                model.tradename2 = tradename2;
                model.tradename3 = tradename3;
                model.atid = atid;               
                lcmyjobs.BLL.enterprise_basic bll = new lcmyjobs.BLL.enterprise_basic();
                if (bll.Update(model))
                {
                    Session["enterprise_name"] = model.name;
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "Info.html");
                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存失败！", "Info.html");
                }
            }



        }
    }
}